﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
namespace QuanLyBanHang
{
    public partial class frmHDBan : Form
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["strConn"].ToString());
        SqlDataAdapter da;
        DataTable dt;
        SqlCommand cmd;
        public frmHDBan()
        {
            InitializeComponent();
        }

        private void frmHDBan_Load(object sender, EventArgs e)
        {
            loadHD();
            loadMaNV();
            loadLTBHang();
        }
        void loadHD()
        {
            da = new SqlDataAdapter(@"select HoaDonBan.MaHDB,MaNV,NgayBan,HangHoa.MaHang,CT_HoaDonBan.SoLuong,
                                        SUM(GiaBan * CT_HoaDonBan.SoLuong) as TongTien from HoaDonBan inner join CT_HoaDonBan
                                        on HoaDonBan.MaHDB = CT_HoaDonBan.MaHDB inner join HangHoa
                                        on HangHoa.MaHang = CT_HoaDonBan.MaHang
                                        group by HoaDonBan.MaHDB,MaNV,NgayBan,HangHoa.MaHang,CT_HoaDonBan.SoLuong", con);
            dt = new DataTable();
            da.Fill(dt);
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                DataRow r = dt.Rows[i];
                dgvHD.Rows.Add(r[0],r[1],r[2],r[3],r[4],r[5]);
            }
        }
        private void ltbHang_SelectedValueChanged(object sender, EventArgs e)
        {
            txtMa.Text = ltbHang.SelectedValue.ToString();
        }
        void TongThanhToan()
        {
            con.Open();
            cmd = new SqlCommand("select DonGia from HangHoa where MaHH=@Ma", con);
            cmd.Parameters.AddWithValue("@Ma", txtMa.Text);
            cmd.ExecuteNonQuery();
            int DonGia = Convert.ToInt32(cmd.ExecuteScalar());

            int sl = Convert.ToInt32(nudSL.Value);
            int tong;
            tong = DonGia * sl;
            txtTongTien.Text = Convert.ToString(tong);
            con.Close();
        }
        void loadMaNV()
        {
            da = new SqlDataAdapter("Select * from NhanVien", con);
            dt = new DataTable();
            da.Fill(dt);
            cboMaNV.DataSource = dt;
            cboMaNV.DisplayMember = "TenNV";
            cboMaNV.ValueMember = "MaNV";
        }
        
        void loadLTBHang()
        {
            da = new SqlDataAdapter("Select * from HangHoa", con);
            dt = new DataTable();
            da.Fill(dt);
            ltbHang.DataSource = dt;
            ltbHang.DisplayMember = "TenHH";
            ltbHang.ValueMember = "MaHH";
        }
        private int checkMa(string Ma)
        {
            int Duyet = 0;
            for (int i = 0; i < dgvHD.Rows.Count; i++)
            {
                if (Ma == dgvHD.Rows[i].Cells[0].Value.ToString())
                {
                    Duyet = 1;
                    break;
                }
            }
            return Duyet;
        }
        bool KTTtrong()
        {
            return txtMaHD.Text.Trim() == "" ||
                txtMa.Text.Trim() == "" ||
                
                cboMaNV.SelectedValue.ToString().Trim() == "" ||
                nudSL.Value == 0 ||
                txtTongTien.Text.Trim() == "";
        }

        private void btnThem_Click(object sender, EventArgs e)
        {
            TongThanhToan();
            try
            {
                if (KTTtrong())
                {
                    MessageBox.Show("Bạn phải điền đầy đủ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else if (checkMa(txtMaHD.Text) != 0)
                {
                    MessageBox.Show("Đã trùng mã, vui lòng chọn mã khác");
                }
                else
                {
                    con.Open();
                    cmd = new SqlCommand("Insert into HoaDon(MaHD, NgayLap, MaNV, TongThanhToan) values(@MaHD, @Ngay, @MaNV, @TT)", con);
                    cmd.Parameters.AddWithValue("@MaHD", txtMaHD.Text);
                    cmd.Parameters.AddWithValue("@Ngay", dtpNgayLap.Value);
                    cmd.Parameters.AddWithValue("@MaNV", cboMaNV.SelectedValue);
                    cmd.Parameters.AddWithValue("@TT", txtTongTien.Text);
                    cmd.ExecuteNonQuery();
                    cmd = new SqlCommand("Insert into CTHoaDon(MaHD, MaHH, SoLuong) values(@MaHD, @MaHH, @SL)", con);
                    cmd.Parameters.AddWithValue("@MaHD", txtMaHD.Text);
                    cmd.Parameters.AddWithValue("@MaHH", txtMa.Text);
                    cmd.Parameters.AddWithValue("@SL", nudSL.Value);
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            dgvHD.Rows.Clear();
            loadHD();
            dgvHD.ClearSelection();
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
        }

        private void btnSua_Click(object sender, EventArgs e)
        {
            TongThanhToan();
            try
            {
                con.Open();
                cmd = new SqlCommand("Update CTHoaDon set MaHH=@MaHH, SoLuong=@SL where MaHD=@MaHD", con);
                cmd.Parameters.AddWithValue("@MaHD", txtMaHD.Text);
                cmd.Parameters.AddWithValue("@MaHH", txtMa.Text);
                cmd.Parameters.AddWithValue("@SL", nudSL.Value);
                cmd.ExecuteNonQuery();

                cmd = new SqlCommand("Update HoaDon set MaNV=@MaNV, NgayLap=@Ngay, TongThanhToan=@TT where MaHD=@MaHD", con);
                cmd.Parameters.AddWithValue("@MaHD", txtMaHD.Text);
                cmd.Parameters.AddWithValue("@Ngay", dtpNgayLap.Value);
                cmd.Parameters.AddWithValue("@MaNV", cboMaNV.SelectedValue);
                cmd.Parameters.AddWithValue("@TT", txtTongTien.Text);
                cmd.ExecuteNonQuery();
                con.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            dgvHD.Rows.Clear();
            loadHD();
            dgvHD.ClearSelection();
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
        }

        private void btnXoa_Click(object sender, EventArgs e)
        {
            TongThanhToan();
            try
            {
                con.Open();
                cmd = new SqlCommand("Delete from CTHoaDon where MaHD=@Ma", con);
                cmd.Parameters.AddWithValue("@Ma", txtMaHD.Text);
                cmd.ExecuteNonQuery();

                cmd = new SqlCommand("Delete from HoaDon where MaHD=@Ma", con);
                cmd.Parameters.AddWithValue("@Ma", txtMaHD.Text);
                cmd.ExecuteNonQuery();
                con.Close();

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            dgvHD.Rows.Clear();
            loadHD();
            dgvHD.ClearSelection();
            btnSua.Enabled = false;
            btnXoa.Enabled = false;
        }

        private void dgvHD_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            txtMaHD.Text = dgvHD.CurrentRow.Cells[0].Value.ToString();
            dtpNgayLap.Value = DateTime.Parse(dgvHD.CurrentRow.Cells[1].Value.ToString());
            cboMaNV.Text = dgvHD.CurrentRow.Cells[2].Value.ToString();
            txtMa.Text = dgvHD.CurrentRow.Cells[4].Value.ToString();
            nudSL.Text = dgvHD.CurrentRow.Cells[5].Value.ToString();
            txtTongTien.Text = dgvHD.CurrentRow.Cells[6].Value.ToString();

            btnSua.Enabled = true;
            btnXoa.Enabled = true;
        }



    }
}
